﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;

namespace Extjs_ReGSharpTest
{
	internal class InputBuilder
	{
		#region Data Memebers

		private NameValueCollection m_NameValueCollection = new NameValueCollection();
		private int m_FilteringCriteriaIndex = 0;

		#endregion

		#region Public Methods

		public InputBuilder AddFilteringParameter(string fieldName, string fieldType, string fieldComparison, string fieldValue)
		{
			const string FilterFieldNameParameterFormat = "filter[{0}][field]";
			const string FilterFieldTypeParameterFormat = "filter[{0}][data][type]";
			const string FilterFieldComparisonParameterFormat = "filter[{0}][data][comparison]";
			const string FilterFieldValueParameterFormat = "filter[{0}][data][value]";

			string nameAssigned = string.Format(FilterFieldNameParameterFormat, m_FilteringCriteriaIndex);
			string typeAssigned = string.Format(FilterFieldTypeParameterFormat, m_FilteringCriteriaIndex);
			string comparisonAssigned = string.Format(FilterFieldComparisonParameterFormat, m_FilteringCriteriaIndex);
			string valueAssigned = string.Format(FilterFieldValueParameterFormat, m_FilteringCriteriaIndex);

			m_FilteringCriteriaIndex++;

			m_NameValueCollection.Add(nameAssigned, fieldName);
			m_NameValueCollection.Add(typeAssigned, fieldType);
			m_NameValueCollection.Add(comparisonAssigned, fieldComparison);
			m_NameValueCollection.Add(valueAssigned, fieldValue);

			return this;
		}

		public InputBuilder AddSortingParameters(string fieldName, string orderDirection)
		{
			const string SortingFieldParameterFormat = "sort";
			const string SortingOrderParameterFormat = "dir";

			m_NameValueCollection.Add(SortingFieldParameterFormat, fieldName);
			m_NameValueCollection.Add(SortingOrderParameterFormat, orderDirection);

			return this;
		}

		public InputBuilder AddPagingParameters(int startIndex, int count)
		{
			const string ItemsStartParameterFormat = "start";
			const string ItemsLimitParameterFormat = "limit";

			m_NameValueCollection.Add(ItemsStartParameterFormat, startIndex.ToString());
			m_NameValueCollection.Add(ItemsLimitParameterFormat, count.ToString());

			return this;
		}

		public NameValueCollection Build()
		{
			return m_NameValueCollection;
		}

		#endregion


	}
}
